package br.great.DAO;

import java.lang.String;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.classes.TexteRange;
import br.great.beans.Tag;

/**
 * PhotoMap
 * Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
 */
/**
 * @author Developpement of PhotoMap
 */
public class DAO_TAG {

    
    private static volatile DAO_TAG instaceDAO_TAG = new DAO_TAG();
    public static String stopWordsList[]={" ","","de","le","la","les","du","pour","par","un","une","des","pour","et","ou","a","au","aux","of","the","an","to"}; 

    /** The private constructor of DAO_TAG for singleton
     */
    private PreparedStatement insertStatement;
    private DAO_TAG() {
        try {
            PgConnect conn = PgConnect.getInstance();
            insertStatement = conn.executeInsertWithPreparedStatement(getInsertStatement());
        } catch (Exception ex) {
            System.err.println("Problems to prepare statement");
        }
    }

    public static boolean isStopWord(String tag){
        if(tag.length()==1){
            return true;
        }
        int taille=stopWordsList.length;
        for(int i=0; i<taille; i++){
            if(stopWordsList[i].equalsIgnoreCase(tag)){
                return true;
            }
        }        
        return false;
    }
    /** The method for get the instance of the class DAO_TAG
     * @return DAO_TAG instaceDAO_TAG
     */
    public static DAO_TAG getInstance() {
        //if(instaceDAO_TAG==null)
        synchronized (DAO_TAG.class) {
            if (instaceDAO_TAG == null) {
                instaceDAO_TAG = new DAO_TAG();
            }
        }
        return instaceDAO_TAG;
    }
    int lTag;

    public String getInsertStatement(){
        String insertTag= "insert into TAG values(nextval('seqmax_tag'),?,1,?,?);";
        return  insertTag; 
    }
    public int insertTAG(Tag tag) {
        int counter=0;
        PgConnect conn = PgConnect.getInstance();
        
        if (tag != null) {
            String tagValueEntier = tag.getTAG_VALUE();
            if (!tagValueEntier.contains("null")) {                                
                    String tagValue = "";
                    tagValue = TexteRange.tradeText(tagValueEntier);
                    tagValue = TexteRange.changeFirstLetter(tagValue);

                 //   System.out.println("tagValueEntiere here is : " + tagValue);
                    // get tag for looking for the tag is existe or not
                    String getTag = "select * from TAG where Lower(tag_value)=Lower('" + tagValue + "') AND Lower(TAG_TYPE)=Lower('"+tag.getTAG_TYPE()+"') AND Lower(TAG_SUBTYPE)=Lower('"+tag.getTAG_SUB_TYPE()+"') order by tag_id;";
               //     String getTag = "select * from TAG where Lower(tag_value)=Lower('" + tagValue + "') order by tag_id;";

                    // insert into the Tag a new value
                    String getIdTag = "select last_value as max from seqmax_tag";

                    try {
                        boolean newTag = false;
                        ResultSet rs_getTag = conn.executeQuery(getTag);
                   //     System.out.println("Tag request : "+getTag);
                        // if the tag is in the table
                        if (rs_getTag.next()) {
                            // after do the insert, get the value of the ID Tag
                            counter = rs_getTag.getInt("TAG_ID");                            
                            String updateCounter = "update TAG set tag_counter=tag_counter+1 where tag_id=" + counter + ";";
                            conn.executeInsert(updateCounter);                            
                        } else {
                            newTag = true;

                        }
                        if (newTag) {

                            // if this tag isn't in table, we insert the tag in it

                            
                            insertStatement.setString(1, tagValue);
                            insertStatement.setString(2, tag.getTAG_TYPE());
                            insertStatement.setString(3, tag.getTAG_SUB_TYPE());
                            insertStatement.execute();
                           // String insertTag = "insert into TAG values(nextval('seqmax_tag'),'" + tagValue + "',1,'" + tag.getTAG_TYPE() + "','"+tag.getTAG_SUB_TYPE()+"');";
                            //conn.executeInsert(insertTag);
                            //Calculatig the max id of the table tag
                            try {
                                ResultSet rs = conn.executeQuery(getIdTag);
                                if (rs.next()) {
                                    counter = rs.getInt("max");
                                }else{
                                    counter=0;
                                }    
                            } catch (Exception e) {
                                //First TAG
                                e.printStackTrace();
                                counter = 0;
                            }      
                           // System.err.println(insertTag);
                            //System.err.println(counter);
                        }
                    } catch (Exception ex) {
                        System.err.println("error of insert Tag with prepared Statement");
                        ex.printStackTrace();
                    }
                  }// End of if                      
            }// End of if        
        conn.Close();
        return counter;
    } 
//                } else {
//                    for (int j = 0; j < tags.length + 1; j++) {
//                        String tagValue = "";
//                        if (j == tags.length) {
//                            tagValue = tagValueEntier;
//                        } else {
//                            tagValue = tags[j];
//                        }
//
//                        tagValue = TexteRange.tradeText(tagValue);
//                        tagValue = TexteRange.changeFirstLetter(tagValue);
//
//System.out.println("tagValuePartielle here is : " + tagValue);
//                        // get tag for looking for the tag is existe or not
//                        String getTag = "select * from TAG where Lower(tag_value)=Lower('" + tagValue + "') AND Lower(TAG_TYPE)=Lower('"+tag.getTAG_TYPE()+"') order by tag_id;";
//
//                        // insert into the Tag a new value
//                        String getIdTag = "select last_value as max from seqmax_tag";
//
//                        try {
//                            boolean newTag = false;
//                            ResultSet rs_getTag = conn.executeQuery(getTag);
//                            System.out.println("Tag request : "+getTag);
//                            // if the tag is in the table
//                            if (rs_getTag.next()) {
//                                // after do the insert, get the value of the ID Tag
//                                counter = rs_getTag.getInt("TAG_ID");                              
//                                String updateCounter = "update TAG set tag_counter=tag_counter+1 where tag_id=" + counter + ";";
//                                conn.executeInsert(updateCounter);                                
//                            } else {
//                                newTag = true;
//
//                            }
//                            if (newTag) {
//
//                                // if this tag isn't in table, we insert the tag in it
//
//                                //Calculatig the max id of the table tag
//                                try {
//                                    ResultSet rs = conn.executeQuery(getIdTag);
//                                    while (rs.next()) {
//                                        counter = rs.getInt("max");
//                                    }
//                                } catch (Exception e) {
//                                    //First TAG
//                                    e.printStackTrace();
//                                    counter = 0;
//                                }
//                                counter = counter + 1;
//
//                                String insertTag = "insert into TAG values(nextval('seqmax_tag'),'" + tagValue + "',1,'" + tag.getTAG_TYPE() + "');";
//                                conn.executeInsert(insertTag);
//                            }
//                        } catch (SQLException ex) {
//                            System.out.println("error of insert Tag");
//                            ex.getStackTrace();
//                        }
//                    }
               

    public void insertFriendsTags(Vector<String> vsFriends, int iCA) {
        DAO_ContentAnnotationHasTag dcah = DAO_ContentAnnotationHasTag.getInstance();
        int weight = 1;
        Tag tag = new Tag();
        for (int k = 0; k < vsFriends.size(); k++) {
            tag.setTAG_VALUE(vsFriends.get(k));
            tag.setTAG_TYPE("FRIEND");
            tag.setTAG_SUB_TYPE("FRIEND");
            int iTag = this.insertTAG(tag);
            dcah.insertContentAnnotationHasTag(iTag, iCA, weight);
        }
    }
    public static void main(String[] args) {
        //System.out.println("scsqcqdsc");
        DAO_TAG dt = DAO_TAG.getInstance();
        Tag tag = new Tag();
        tag.setTAG_VALUE("PARIS");
        tag.setTAG_TYPE("loca_city");
     //   System.err.println("Tagid:"+dt.insertTAG(tag));
    }
}
